package com.lwei.tallybook.client.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwei.tallybook.base.domain.Account;
import com.lwei.tallybook.base.domain.AccountBook;
import com.lwei.tallybook.base.dto.Message;
import com.lwei.tallybook.service.service.AccountBookBillService;
import com.lwei.tallybook.service.service.AccountBookService;
import com.lwei.tallybook.service.service.RelBillBookService;
import com.lwei.tallybook.service.service.RelBookAccountService;
import com.lwei.tallybook.service.vo.response.BookResponseVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * 账本控制层
 * @author sks.lwei
 */
@RestController
@RequestMapping("/api/book")
public class BookController extends BaseController {

    /**
     * 账本
     */
    @Resource
    private AccountBookService accountBookService;

    /**
     * 列出我的账本
     */
    @RequestMapping("/list-my-book")
    public Message listMyAccountBook(Page<AccountBook> page){
        Account currentAccount = currentUser();
        Page<BookResponseVO> pages = (Page<BookResponseVO>) accountBookService.listAccountBookByOwner(page, currentAccount.getUuid());
        Message pageData = Message.page(pages);
        //标记是否有下一页
        pageData.setStatus(pages.hasNext()?"next":"");
        return pageData;
    }

    /**
     * 列出指定用户所属的账本
     */
    @RequestMapping("/list-book-by-own")
    public Message listBookByOwn(Page<AccountBook> page, @RequestParam("uuid") String ownUuid){
        IPage<BookResponseVO> bookPages = accountBookService.listAccountBookByOwner(page, ownUuid);
        return Message.page(bookPages);
    }

    /**
     * 列出我参与的账本
     */
    @RequestMapping("/list-my-book-join")
    public Message listMyJoinBook(Page<AccountBook> page){
        Account currentUser = currentUser();
        IPage<BookResponseVO> bookPages = accountBookService.listAccountBookByJoin(page, currentUser.getUuid());
        return Message.page(bookPages);
    }

    /**
     * 列出指定用户参与的账本
     */
    @RequestMapping("/list-book-by-join")
    public Message listBookByJoin(Page<AccountBook> page, @RequestParam("uuid") String joinUuid){
        IPage<BookResponseVO> bookPages = accountBookService.listAccountBookByJoin(page, joinUuid);
        return Message.page(bookPages);
    }

    /**
     * 列出指定账单的账本
     */
    @RequestMapping("/list-book-by-bill")
    public Message listBookByBill(Page<AccountBook> page, @RequestParam("uuid") String billUuid){
        Page<BookResponseVO> pages = (Page<BookResponseVO>) accountBookService.listBookByBill(page, billUuid);
        Message pageData = Message.page(pages);
        //标记是否有下一页
        pageData.setStatus(pages.hasNext()?"next":"");
        return pageData;
    }

    /**
     * 列出指定账单的账本
     */
    @RequestMapping("/list-book-by-bill-join")
    public Message listBookByBillWithJoin(Page<AccountBook> page, @RequestParam("uuid") String billUuid){
        Account currentUser = currentUser();
        Page<BookResponseVO> pages = (Page<BookResponseVO>) accountBookService.listBookByBillWhitJoin(page, billUuid, currentUser.getUuid());
        Message pageData = Message.page(pages);
        //标记是否有下一页
        pageData.setStatus(pages.hasNext()?"next":"");
        return pageData;
    }

    /**
     * 添加账本
     */
    @RequestMapping(value = "/add-book", method = RequestMethod.POST)
    public Message add(AccountBook accountBook){
        if(accountBook.getConsumeParticipator().length == 0){
            return Message.failMsg("参与人不能为空");
        }
        String uuid = accountBookService.saveAccountBook(accountBook);
        return Message.data(uuid);
    }

    /**
     * 展示账本详情
     */
    @RequestMapping("/show-book")
    public Message show(String uuid){
        AccountBook book = accountBookService.getAccountBookByUuid(uuid);
        return Message.data(book);
    }

    /**
     * 修改账本
     */
    @PostMapping("/edit-book")
    public Message edit(AccountBook accountBook){
        accountBookService.editAccountBook(accountBook);
        return Message.SUCCESS;
    }

    /**
     * 批量删除账本
     */
    @GetMapping("/del-book")
    public Message remove(@RequestParam("uuids") String[] bookUuid){
        accountBookService.removeAccountBook(Arrays.asList(bookUuid));
        //删除账本不会删除账单
        return Message.SUCCESS;
    }

}
